package com.own.demo.建筑物;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.jts.生成osm;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.vo.Bounds;
import org.locationtech.jts.geom.Coordinate;

import java.io.BufferedWriter;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 建筑物统计v1 {

    static int floorHeight = 3;
    private static String type = "Polygon";
    static String geoType = "LineString";
    static String dataArea = "112.854444,28.275559;112.882883,28.306045;112.923068,28.301691;112.975618,28.309311;113.035586,28.309311;113.072062,28.301691;113.086900,28.287537;113.107302,28.280459;113.111629,28.239619;113.111011,28.212383;113.089991,28.181325;113.062789,28.144262;113.053515,28.129543;112.977472,28.117002;112.908230,28.138266;112.861244,28.181325;112.846407,28.243976;112.844552,28.245610;112.844552,28.245610;112.854444,28.275559";

    static String urlOSM = "E:\\ownCode\\UMA\\数据\\路网数据\\base\\楼层\\osm\\";
//    static String buld = "112.671689,28.246673;112.750619,28.295456;112.836473,28.239354;112.826780,28.166133;112.727078,28.150262;112.632916,28.164912;112.671689,28.246673&113.722709,28.356403;113.823796,28.347873;113.809948,28.26131;113.724094,28.313744;113.722709,28.356403";

    static Map<Integer, JSONArray> floorMap = new HashMap(1024);
    static Map<Integer, JSONArray> floorHeightMap = new HashMap(1024);

    static {
        floorHeightMap.put(-1, new JSONArray());
        for (int i = 1; i < 9; i++) {
            floorHeightMap.put(i, new JSONArray());
        }
    }

    public static void main(String[] args) {
        countInfo();

        Bounds boundingBox = new Bounds(114.260085, 114.260085, 27.848061, 28.661844);

        floorHeightMap.remove(-1);

//        writerFoFile()
        Map<Integer, JSONArray> temMap = new HashMap<>(102);
        temMap.put(1, floorHeightMap.get(1));
//        temMap.put(2, floorHeightMap.get(2));
//        temMap.put(3, floorHeightMap.get(3));
//        temMap.put(4, floorHeightMap.get(4));
//        temMap.put(5, floorHeightMap.get(5));
//        temMap.put(6, floorHeightMap.get(6));
//        temMap.put(7, floorHeightMap.get(7));
//        temMap.put(8, floorHeightMap.get(8));
        JSONObject jsonObject = getParent();

        int index = 1;
        for (Map.Entry<Integer, JSONArray> entry : temMap.entrySet()) {
            addLingString(jsonObject.getJSONArray("features"), geoType, entry.getValue());
            index = 1 + index;
        }
        String json = JSONObject.toJSONString(jsonObject);
        writerFoFile("var polylineData1 = " + json, "E:\\ownCode\\UMA\\uam\\地图工具-路网\\", "polylineData1.js");
        writerFoFile(json, "E:\\ownCode\\UMA\\uam\\地图工具-路网", "40米以上建筑物.geojson");


    }

    public static void addLingString(JSONArray jsonArray, String geoType, JSONArray gpsList) {
        int row = gpsList.size();
        for (int i = 0; i < row; i++) {
            JSONObject jsonObject = gpsList.getJSONObject(i);

            JSONObject geo = new JSONObject(4);
            geo.put("type", "Feature");
            JSONObject geoPro = new JSONObject(1);
            geoPro.put("Floor", jsonObject.getInteger("Floor"));
            geo.put("properties", geoPro);

            JSONObject objectInfo = new JSONObject();
            objectInfo.put("coordinates", jsonObject.getJSONArray("coordinates"));
            objectInfo.put("type", geoType);
            geo.put("geometry", objectInfo);
            jsonArray.add(geo);
        }
    }


    public static void addLingStringOne(JSONArray jsonArray, String geoType, JSONArray gpsList) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("Floor", geo.getInteger("Floor"));
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        jsonArray.add(geo);
    }

    public static JSONArray setXYGeo(JSONArray jsonArray, List<Coordinate> list, String geoType) {
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        JSONArray gpsList = new JSONArray(coordRow);
        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        addLingStringOne(jsonArray, geoType, gpsList);
        return gpsList;
    }

    private static JSONObject getParent() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "GeometryCollection");
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("features", jsonArray);
        return jsonObject;
    }

    public static void writerFoFile(String data, String folderPath, String fileName) {
        try {
            File folder = new File(folderPath);

            if (!folder.exists()) {
                folder.mkdirs();
            }
            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
            CommWriterFileInfo.writer(bufferedWriter, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<Integer, JSONArray> countInfo() {
        File geoZJ = new File("E:\\ownCode\\emg-geojson - 副本\\src\\test\\java\\com\\own\\demo\\建筑物\\building.geojson");
        JSONObject geoData = JSONObject.parseObject(FileToStringUtils.txt2String(geoZJ));

        JSONArray jsonArray = geoData.getJSONArray("features");

        int row = jsonArray.size();

        for (int i = 0; i < row; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject properties = jsonObject.getJSONObject("properties");

            Integer floor = properties.getInteger("Floor");

            JSONObject geometry = jsonObject.getJSONObject("geometry");
            geometry.put("Floor", floor);


            JSONArray floorArr = floorMap.get(floor);
            if (floorArr == null) {
                floorArr = new JSONArray(2024);
                floorMap.put(floor, floorArr);
            }
            floorArr.add(geometry);
        }
        initData();
        System.out.println(floorMap.size());


        int allData = 0;
        for (Map.Entry<Integer, JSONArray> data : floorHeightMap.entrySet()) {
            allData = allData + data.getValue().size();
        }
        System.out.println("总数据：" + row + ",统计数据：" + allData);
        System.out.println("每一层数据量");
        for (Map.Entry<Integer, JSONArray> data : floorHeightMap.entrySet()) {
            System.out.println("第" + data.getKey() + "层, 数量：" + data.getValue().size());
        }
        return floorHeightMap;
    }

    private static void setMap(int key, JSONArray jsonArray) {
        floorHeightMap.get(key).addAll(jsonArray);
    }

    private static void initData() {
        for (Map.Entry<Integer, JSONArray> data : floorMap.entrySet()) {
            int floor = data.getKey();
            int height = floor * floorHeight;
            if (height < 40) {
                setMap(-1, data.getValue());
                continue;
            }

            //第一层：
            if (height <= 60) {
                setMap(1, data.getValue());
                continue;
            }

            if (height <= 80) {
                setMap(2, data.getValue());
                continue;
            }

            if (height <= 100) {
                setMap(3, data.getValue());
                continue;
            }

            if (height <= 120) {
                setMap(4, data.getValue());
                continue;
            }

            if (height <= 140) {
                setMap(5, data.getValue());
                continue;
            }

            if (height <= 160) {
                setMap(6, data.getValue());
                continue;
            }

            if (height <= 180) {
                setMap(7, data.getValue());
                continue;
            }

            if (height <= 200) {
                setMap(8, data.getValue());
                continue;
            }
        }

    }
}
